package xin.lz1998.cq.plugin.customize;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xin.lz1998.cq.constant.TitleConstant;
import xin.lz1998.cq.event.message.CQGroupMessageEvent;
import xin.lz1998.cq.robot.CQPlugin;
import xin.lz1998.cq.robot.CoolQ;

/**
 * 设置专属头衔
 */
@Component
@Order(3)//插件执行顺序
public class GroupSpecialTitlePlugin extends CQPlugin {

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg=event.getMessage().trim() ;
        long groupId = event.getGroupId();
        String[] arr = msg.split(" ");
        if(TitleConstant.GROUPID==groupId&&msg.startsWith(TitleConstant.HEARD)&&arr.length>1){
            if(arr.length==2) {
                if (arr[1].startsWith(TitleConstant.ADMINHEARD)&&event.getUserId()!=TitleConstant.ADMINID) {
                    cq.setGroupSpecialTitle(groupId, event.getUserId(), TitleConstant.OTHERHEARD, true);
                } else {
                    cq.setGroupSpecialTitle(groupId, event.getUserId(), arr[1], true);
                }
            }
            if(arr.length==3){
                try {
                    if(arr[1].startsWith("[")){
                        String[] idArr = arr[1].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                        Long adminId = event.getUserId();
                        if(user_id==TitleConstant.ADMINID){
                            cq.setGroupSpecialTitle(groupId, adminId, TitleConstant.CURRENTHEARD, true);
                        }else{
                            if(adminId!=TitleConstant.ADMINID&&arr[2].startsWith(TitleConstant.ADMINHEARD)){
                                cq.setGroupSpecialTitle(groupId, adminId, TitleConstant.OTHERHEARD, true);
                            }else {
                                cq.setGroupSpecialTitle(groupId, user_id, arr[2], true);
                            }
                        }
                    }else{
                        cq.setGroupSpecialTitle(groupId,event.getUserId(),TitleConstant.ZHUHEARD,true);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return MESSAGE_IGNORE;
    }
}
